package com.foot.travel.viewholder;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

import com.foot.travel.R;
import com.foot.travel.activity.SchoolTravelDetailActivity;
import com.koalac.ironman.common.adapter.ViewHolderModel;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.ClickNumber;
import com.koalac.ironman.net.pojo.request.DeleteNumber;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.net.pojo.response.SchoolTravelItem;
import com.koalac.ironman.util.ScreenInfoUtil;
import com.koalac.ironman.widget.ImageLoadAgreement.ImageType;
import com.koalac.ironman.widget.LifeqImageView;

public class OtherClassTravelViewHolder extends ViewHolderModel<SchoolTravelItem>{
    
    private TextView titleView;
    private LifeqImageView imageView;
    private TextView likeView;
    
    private TextView numberOfPeopleView;
    private TextView privilegePriceVeiw;
    
    
    public OtherClassTravelViewHolder(Context context,
	    List<SchoolTravelItem> list) {
	super(context, list);
    }

    @Override
    public void initViewHoler(int position, View convertView) {
	titleView = (TextView)convertView.findViewById(R.id.title);
	imageView = (LifeqImageView)convertView.findViewById(R.id.hot_recommend_image);
	RelativeLayout.LayoutParams imageLp = (LayoutParams) imageView.getLayoutParams();
	imageLp.height = ScreenInfoUtil.getScreenWidth(context) * 9 / 16;
	imageView.setLayoutParams(imageLp);
	likeView = (TextView)convertView.findViewById(R.id.like_view);
	
	numberOfPeopleView = (TextView)convertView.findViewById(R.id.number_of_people);
	privilegePriceVeiw = (TextView)convertView.findViewById(R.id.privilege_price);
    }

    @Override
    public void setViewHolerValues(int position, View convertView) {
	final SchoolTravelItem schoolTravelItem = list.get(position);
	titleView.setText(schoolTravelItem.getName());
	imageView.loadRoundImage(schoolTravelItem.getHeadImgUrl(), ImageType.SourceImage);
	likeView.setText(schoolTravelItem.getFollowNumber()+"人关注");
	
	numberOfPeopleView.setText("/" + schoolTravelItem.getFollowNumber() +"人");
	privilegePriceVeiw.setText("¥" + schoolTravelItem.getAfterPrice());
	convertView.setOnClickListener(new OnClickListener() {
	    
	    @Override
	    public void onClick(View v) {
		Intent intent = new Intent(context,
			SchoolTravelDetailActivity.class);
		intent.putExtra(SchoolTravelDetailActivity.SCHOOL_TRAVEL_DETAIL_ID,
			schoolTravelItem.getId());
		context.startActivity(intent);
	    }
	});
	
	if (schoolTravelItem.getNumber_flag() == SchoolTravelItem.ATTENTION) {
	    likeView.setSelected(true);
	}else {
	    likeView.setSelected(false);
	}
	
	
	likeView.setOnClickListener(new OnClickListener() {
	    
	    @Override
	    public void onClick(View v) {
		 if (schoolTravelItem.getNumber_flag() == SchoolTravelItem.ATTENTION) {
		     cancelAttention(schoolTravelItem);
		}else {
		    payAttention(schoolTravelItem);
		}
	    }
	});
    }
    
    /**
     * 关注
     * @param schoolTravelItem
     */
    private void payAttention(final SchoolTravelItem schoolTravelItem) {
	ClickNumber clickNumber = new ClickNumber();
	clickNumber.setId(schoolTravelItem.getId());
	MyRequest.request(context, clickNumber, new ResponseCallback() {
	    
	    @Override
	    public void process(boolean result, Context context, Response data,
		    String msg) {
			if (result) {
			    int followNumber = Integer.parseInt(schoolTravelItem.getFollowNumber()) + 1;
			    schoolTravelItem.setFollowNumber(""+followNumber);
			    likeView.setText(schoolTravelItem.getFollowNumber()+"人关注");
			    likeView.setSelected(true);
			    schoolTravelItem.setNumber_flag(SchoolTravelItem.ATTENTION);
			}
	    }
	});
    }
    /**
     * 取消关注
     * @param schoolTravelItem
     */
    private void cancelAttention(final SchoolTravelItem schoolTravelItem) {
	DeleteNumber deleteNumber = new DeleteNumber();
	deleteNumber.setId(schoolTravelItem.getId());
	MyRequest.request(context, deleteNumber, new ResponseCallback() {
	    
	    @Override
	    public void process(boolean result, Context context, Response data,
		    String msg) {
			if (result) {
			     int followNumber = Integer.parseInt(schoolTravelItem.getFollowNumber()) -1;
			    schoolTravelItem.setFollowNumber(""+followNumber);
			    likeView.setText(schoolTravelItem.getFollowNumber()+"人关注");
			    likeView.setSelected(false);
			    schoolTravelItem.setNumber_flag(SchoolTravelItem.NO_ATTENTION);
			}
	    }
	});
    }

}
